﻿

@page "/scan"

<h3>Scan</h3>
@switch (pageState)
{
    case PageState.Options:
        <div class="runid-box">
            <label for="RunId">Run ID:</label>
            <input id="RunId" type="text" @bind="RunIdInput" />
            <input id="UseTimestampCheckbox" type="checkbox" @bind="UseTimestamp" />
            <label for="UseTimestampCheckbox">Use Timestamp</label>
            <button class="start-scan-button" @onclick="BeginScan">Start Scan</button>
        </div>
        <div class="options-pane">
            <span class="oi oi-wrench options-icon" aria-hidden="true"></span>  Collector Options
            <hr />
            <CollectorOptionsRazor />
        </div>
        break;
    case PageState.Scanning:
        <Scanning />
        break;
    case PageState.Finished:
        <button @onclick="GoToOptions">Scan Again</button>
        break;
    case PageState.Error:
        <p>An Error Occured while collecting.</p>
        break;
}
@code {
    enum PageState
    {
        Options,
        Scanning,
        Finished,
        Error
    }
    bool UseTimestamp = true;
    string RunIdInput = "Enter Run ID";

    PageState pageState = PageState.Options;

    System.Threading.Timer timer;

    async void BeginScan()
    {
        appData.RunId = UseTimestamp ? DateTime.Now.ToString() : RunIdInput;
        timer = new System.Threading.Timer((_) => InvokeAsync(() => StateHasChanged()), null, 0, 100);
        appData.CollectOptions.RunId = appData.FirstRunId;
        pageState = PageState.Scanning;
        this.StateHasChanged();
        if (await Task.Factory.StartNew(() => AttackSurfaceAnalyzerClient.RunCollectCommand(appData.CollectOptions)) == Microsoft.CST.AttackSurfaceAnalyzer.Types.ASA_ERROR.NONE)
        {
            pageState = PageState.Finished;
        }
        else
        {
            pageState = PageState.Error;
        }
        AttackSurfaceAnalyzerClient.ClearCollectors();
    }

    void GoToOptions()
    {
        pageState = PageState.Options;
    }
}
